package com.three.controller.old; /**
 * @author wzm
 * @create 2023-11-07 14:17
 * project_name:web2310
 * @Description
 */


import com.three.pojo.User;
import com.three.service.UserService;
import com.three.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置请求和响应的编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //2.获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //3.将请求参数封装为对象
        User user = new User(null,username,password);

        //4.访问Service进行操作
        //验证用户名是否存在
        boolean exists = userService.existsName(username);
        PrintWriter writer = response.getWriter();

        //5.判断用户对象是否为null
        if(exists){ //返回true不存在
            //用户名不存在
            //执行新增 Servcie
          boolean register = userService.register(user);
          if (register){
              writer.write("注册成功");
          }else{
              writer.write("注册失败");
          }

        }else{
            //用户名已经存在 各处提示信息
            writer.write("用户名已经存在，请更换！！");
        }


    }
}
